﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Xinchen.DbUtils;
using Xinchen.SOA.Manager.Api.Contracts;
using Xinchen.Utils;

namespace Xinchen.SOA.GateWay
{
    public class ApplicationManager : Singleton<ApplicationManager>
    {
        IApplicationSerivce BuildService()
        {
            var _service = ServiceBuilder.BuildService<IApplicationSerivce>(ServiceConsts.ServiceUrl, ServiceConsts.ApplicationServiceName);
            return _service;
        }
        public List<ApplicationInfo> GetApplications(params int[] applicationIds)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetApplications().Where(x => applicationIds.Contains(x.Id)).ToList();
            }
        }

        public bool VerifyApplication(int applicationId, string applicationKey)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                var result = _service.VerifyApplication(applicationId, applicationKey);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
                return result.IsOk;
            }
        }
        public PageResult<ApplicationServiceInfo> GetApplicationServices(int page, int pageSize, int applicationId, IList<SqlFilter> filters)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetApplicationServices().Where(x => x.ApplicationId == applicationId).Where(filters).Page(page, pageSize);
            }
        }
        public List<ApplicationServiceInfo> GetApplicationServices(int applicationId)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetApplicationServices().Where(x => x.ApplicationId == applicationId).ToList();
            }
        }

        public List<ApplicationServiceActionInfo> GetApplicationServiceActions(int applicationServiceId)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetApplicationServiceActions().Where(x => x.ApplicationServiceId == applicationServiceId).ToList();
            }
        }
        public ApplicationInfo GetApplication(int applicationId)
        {
            return GetApplications(applicationId).FirstOrDefault();
        }
        public PageResult<ApplicationInfo> GetApplications(int page, int pageSize, IList<SqlFilter> filters)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                return service.GetApplications().Where(filters).Page(page, pageSize);
            }
        }

        public ApplicationInfo GetApplication(string applicationName)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.GetApplication(applicationName);
            }
        }
        public List<ApplicationInfo> SearchApplications(string applicationName)
        {
            var _service = BuildService();
            using (_service as IDisposable)
            {
                return _service.SearchApplications(applicationName);
            }
        }

        public void AddApplication(AddApplicationRequest request)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.AddApplication(request);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void UpdateApplication(UpdateApplicationRequest request)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.UpdateApplication(request);
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }

        public void RemoveApplications(int[] ids)
        {
            var service = BuildService();
            using (service as IDisposable)
            {
                var result = service.RemoveApplications(new RemoveApplicationRequest()
                {
                    Ids = ids
                });
                if (result.ResultCode != 0)
                {
                    throw new ApplicationException(result.ResultMessage);
                }
            }
        }
    }
}
